

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xyz.Film;

/**
 * Servlet implementation class FilmsSortedByName
 */
@WebServlet("/FilmsSortedByName")
public class FilmsSortedByName extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FilmsSortedByName() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession s=request.getSession();
		@SuppressWarnings("unchecked")
		List<Film> films=(List<Film>) s.getAttribute("filmList");
		List<Film> filmsRes=new ArrayList<Film>();
		//----SelectionSorting----
		while(films.size()>0){
			Film minF=films.get(0);
			int minFIndex=0;
			for(int j=1; j<films.size(); j++){
				if (films.get(j).getTitle().compareTo(minF.getTitle())<0){
					minF=films.get(j);
					minFIndex=j;
				}
			}
			filmsRes.add(minF);
			films.remove(minFIndex);
		}
		request.setAttribute("filmList", filmsRes);
		request.setCharacterEncoding("UTF-8");
		RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/jsp/filteredFilms.jsp");
		view.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
